package com.yjc.hire.work.controller;

import com.yjc.hire.grace.result.GraceJSONResult;
import com.yjc.hire.grace.result.PageGridResult;
import com.yjc.hire.grace.result.ResponseStatusEnum;
import com.yjc.hire.utils.LocalDateUtils;
import com.yjc.hire.work.bo.SearchReportJobBO;
import com.yjc.hire.work.service.ReportService;
import com.yjc.mo.ReportMO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.aggregation.ArrayOperators;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * </p>
 *
 * @author jcyin
 * @since 2025/9/3
 * </p>
 */

@RestController
@RequestMapping("report")
public class ReportController {

    @Autowired
    private ReportService reportService;

    @PostMapping("create")
    public GraceJSONResult create(@RequestBody ReportMO reportMO) {
        boolean exist = reportService.isReportRecordExist(reportMO.getReportUserId(), reportMO.getJobId());
        if (exist){
            return GraceJSONResult.errorCustom(ResponseStatusEnum.REPORT_RECORD_EXIST_ERROR);
        }
        reportService.saveReportRecord(reportMO);
        return GraceJSONResult.ok();
    }

    @PostMapping("pageReportRecordList")
    public GraceJSONResult pageReportRecordList(@RequestBody SearchReportJobBO searchReportJobBO,
                                                Integer page,Integer pageSize) {
        if (pageSize == null) pageSize = 10;

        if (page == null) page = 1;
        LocalDate beginDate = searchReportJobBO.getBeginDate();
        LocalDate endDate = searchReportJobBO.getEndDate();
        if (beginDate != null) {

            String beginDateTimeStr =
                    LocalDateUtils.format(beginDate,LocalDateUtils.DATE_PATTERN) + " 00:00:00";
            LocalDateTime beginDateTime = LocalDateUtils.parseLocalDateTime(beginDateTimeStr, LocalDateUtils.DATETIME_PATTERN);
            searchReportJobBO.setBeginDateTime(beginDateTime);
        }
        if (endDate != null) {
            String endDateTimeStr =
                    LocalDateUtils.format(beginDate,LocalDateUtils.DATE_PATTERN) + " 00:00:00";
            LocalDateTime endDateTime = LocalDateUtils.parseLocalDateTime(endDateTimeStr, LocalDateUtils.DATETIME_PATTERN);
            searchReportJobBO.setEndDateTime(endDateTime);
        }

        PageGridResult<List<ReportMO>> result = reportService.pageReportRecordList(searchReportJobBO, page, pageSize);
        return GraceJSONResult.ok(result);
    }


}
